var vehicle_map = function(){ 
    var key = {
        vehicle_id:NumberInt(this.vehicle_id),
        area_id:NumberInt(this.area_id),
        enterprise_id:NumberInt(this.enterprise_id),
        gnss_centerid:NumberInt(this.gnss_centerid),
        trade_kind_id:NumberInt(this.trade_kind_id),
        platform_id:NumberInt(this.platform_id),
        vehicle_color:NumberInt(this.vehicle_color),
        vehicle_no:this.vehicle_no,
        owner_unit:this.owner_unit,
        area_name : this.area_name,
        enterprise_name:this.enterprise_name,
        trade_kind:this.trade_kind,
        platform_name:this.platform_name,
        province_id:NumberInt(this.province_id),
        city_id:NumberInt(this.city_id),
        province_name:this.province_name,
        city_name:this.city_name
    };
    var values = {
    	track_normal:NumberInt(this.track_normal),
    	alarm_count:NumberInt(this.alarm_count),
    	alarm_processed:NumberInt(this.alarm_processed),
    	overspeed_count:NumberInt(this.overspeed_count),
    	overspeed_processed : NumberInt(this.overspeed_processed),
    	roadoffset_count : NumberInt(this.roadoffset_count),
    	roadoffset_processed:NumberInt(this.roadoffset_processed),
    	tired_count:NumberInt(this.tired_count),
    	tired_processed:NumberInt(this.tired_processed),
    	tired_duration:NumberInt(this.tired_duration),
    	prohibit_driving_count:NumberInt(this.prohibit_driving_count),
    	prohibit_driving_processed:NumberInt(this.prohibit_driving_processed),
    	mileage_2_5:NumberInt(this.mileage_2_5),
    	nightly_3rd_count:NumberInt(this.nightly_3rd_count),
    	nightly_3rd_processed:NumberInt(this.nightly_3rd_processed),
    	over_load_count:NumberInt(this.over_load_count),
    	over_load_processed:NumberInt(this.over_load_processed),
    	overspeed_100_count:NumberInt(this.overspeed_100_count),
    	speed_160_count:NumberInt(this.speed_160_count),
    //	track_complete_rate:this.track_complete_rate,
    	//data_fault_rate:this.data_fault_rate,
    	//data_correct_rate:this.data_correct_rate,
    	has_tracks:this.has_track,
    	exception_track_count:NumberInt(this.exception_track_count),
    	gpstime_exception_count:NumberInt(this.gpstime_exception_count),
    	location_exception_count:NumberInt(this.location_exception_count),
    	speed_exception_count:NumberInt(this.speed_exception_count),
    	head_exception_count:NumberInt(this.head_exception_count),
    	has_nets:this.has_net,
    //	notrack_day_count:this.notrack_day_count,
    //  notrack_vehicle_count:this.notrack_vehicle_count,
    //	notrack_day_month_count:this.notrack_day_month_count,
    	sum_gap_time:NumberInt(this.sum_gap_time),
    	sum_total_time:NumberInt(this.sum_total_time)
    };
    emit(key, values);
}; 
var vehicle_reduce = function(key, values){ 
    const reduce_result = {
    		track_normal:0,
        	alarm_count:0,
        	alarm_processed:0,
        	overspeed_count:0,
        	overspeed_processed:0,
        	roadoffset_count:0,
        	roadoffset_processed:0,
        	tired_count:0,
        	tired_processed:0,
        	tired_duration:0,
        	prohibit_driving_count:0,
        	prohibit_driving_processed:0,
        	mileage_2_5:0,
        	nightly_3rd_count:0,
        	nightly_3rd_processed:0,
        	over_load_count:0,
        	over_load_processed:0,
        	overspeed_100_count:0,
        	speed_160_count:0,
        	has_tracks:[],
        	exception_track_count:0,
        	gpstime_exception_count:0,
        	location_exception_count:0,
        	speed_exception_count:0,
        	head_exception_count:0,
        	has_nets:[],
        //	track_complete_rate:0,
        //	notrack_day_count:this.notrack_day_count,
        //  notrack_vehicle_count:this.notrack_vehicle_count,
        //	notrack_day_month_count:this.notrack_day_month_count,
        	sum_gap_time:0,
        	sum_total_time:0
    };

    values.forEach(
        function(val){
            reduce_result.track_normal+=val.track_normal;
            reduce_result.alarm_count+=val.alarm_count;
            reduce_result.alarm_processed+=val.alarm_processed;
            reduce_result.overspeed_count+=val.overspeed_count; 
            reduce_result.overspeed_processed+=val.overspeed_processed;
            reduce_result.roadoffset_count+=val.roadoffset_count; 
            reduce_result.roadoffset_processed+=val.roadoffset_processed;
            reduce_result.tired_count+=val.tired_count; 
            reduce_result.tired_processed+=val.tired_processed;
            reduce_result.tired_duration+=val.tired_duration; 
            reduce_result.prohibit_driving_count+=val.prohibit_driving_count;
            reduce_result.prohibit_driving_processed+=val.prohibit_driving_processed; 
            reduce_result.mileage_2_5+=val.mileage_2_5;
            reduce_result.nightly_3rd_count+=val.nightly_3rd_count; 
            reduce_result.nightly_3rd_processed+=val.nightly_3rd_processed;
            reduce_result.over_load_count+=val.over_load_count; 
            reduce_result.over_load_processed+=val.over_load_processed;
            reduce_result.overspeed_100_count+=val.overspeed_100_count; 
            reduce_result.speed_160_count+=val.speed_160_count;
			
            reduce_result.has_tracks=reduce_result.has_tracks.concat(val.has_tracks); 
            reduce_result.exception_track_count+=val.exception_track_count;
            reduce_result.gpstime_exception_count+=val.gpstime_exception_count; 
            reduce_result.location_exception_count+=val.location_exception_count;
            reduce_result.speed_exception_count+=val.speed_exception_count; 
            reduce_result.head_exception_count+=val.head_exception_count;
           
            reduce_result.has_nets=reduce_result.has_nets.concat(val.has_nets);

            
            reduce_result.sum_gap_time+=val.sum_gap_time; 
            reduce_result.sum_total_time+=val.sum_total_time;
            
        }
    ); 

    return reduce_result; 
}; 

var vehicle_finalize= function(key, values){
	var has_nets=values.has_nets;
    if(has_nets.length>0){
        values.has_net=false;
        for(var i=0;i<has_nets.length;i++){
            if(has_nets[i]==true){
                values.has_net=true;
                break;
            }
        }
    }else{
        values.has_net=values.has_nets;
    }
    delete values.has_nets;
    var has_tracks=values.has_tracks;
    var vehicle_online_count=0;
    if(has_tracks.length>0){
        values.has_track=false;
        for(var i=0;i<has_tracks.length;i++){
            if(has_tracks[i]==true){
            	values.has_track=true;
            	break;
            }
        }
    }else{
        values.has_track=values.has_tracks;
    }
    delete values.has_tracks;
    if(values.track_normal>0){
        values.data_correct_rate = parseFloat(((values.track_normal/(values.track_normal+values.exception_track_count))* 100).toFixed(2));
        values.data_fault_rate = (100-values.data_correct_rate);
    }else{
        values.data_correct_rate=0.0;
        values.data_fault_rate=0.0;
    }
	values.track_complete_rate = values.track_normal>2?parseFloat((1*100 - (values.sum_gap_time /values.sum_total_time)*100).toFixed(2)):0.0;
    return values;
}
    
db.runCommand({
    mapReduce: 'vehicle_swap',
    map: vehicle_map,
    reduce: vehicle_reduce,
    finalize: vehicle_finalize,
    out: {
        replace: 'vehicle_swap_week_temp'
    },
    sort: {
        _id: 1
    },query: {
    	analyse_date :{$gte:ISODate("2017-10-22T16:00:00Z"),$lte:ISODate("2017-10-28T16:00:00Z")}
    }
});